package com.leyou.page.service.impl;

import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.*;
import com.leyou.page.service.GoodsPageService;
import com.leyou.page.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class GoodsPageServiceImpl implements GoodsPageService {

    @Autowired
    private ItemClient itemClient;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String KEY_PREFIX_SPU = "page:spu:id:";
    private static final String KEY_PREFIX_SKU = "page:sku:id:";
    private static final String KEY_PREFIX_DETAIL = "page:detail:id:";
    private static final String KEY_PREFIX_CATEGORY = "page:category:id:";
    private static final String KEY_PREFIX_BRAND = "page:brand:id:";
    private static final String KEY_PREFIX_SPEC = "page:spec:id:";

    @Override
    public String loadSpuData(Long spuId) {
        //根据id查询
        SpuDTO spuDTO = itemClient.querySpuById(spuId);
        //判断是否为空
        if (spuDTO == null || spuDTO.getId() == null) {
            throw new LyException(404, "商品不存在！");
        }
        //转换为vo
        SpuVO spuVO = spuDTO.toEntity(SpuVO.class);
        //获取categoryIds并赋值
        spuVO.setCategoryIds(spuDTO.getCategoryIds());
        //序列化转json
        String toJson = JsonUtils.toJson(spuVO);
        //写入Redis缓存中
        redisTemplate.opsForValue().set(KEY_PREFIX_SPU + spuId,toJson);
        //返回json，省去controller中序列化
        return toJson;
    }

    @Override
    public String loadSpuDetailData(Long spuId) {
        //根据id查询
        SpuDetailDTO spuDetailDTO = itemClient.querySpuDetailById(spuId);
        //判断是否为空
        if (spuDetailDTO == null) {
            throw new LyException(404, "商品不存在！");
        }
        //全都需要，不用转vo
        //序列化转json
        String toJson = JsonUtils.toJson(spuDetailDTO);
        //写入Redis缓存中
        redisTemplate.opsForValue().set(KEY_PREFIX_DETAIL + spuId,toJson);
        //返回json，省去controller中序列化
        return toJson;
    }

    @Override
    public String loadSkuListData(Long spuId) {
        //根据id查询
        List<SkuDTO> skuDTOS = itemClient.querySkuBySpuId(spuId);
        //判断是否为空
        if (skuDTOS == null || skuDTOS.size() <= 0) {
            throw new LyException(404, "商品不存在！");
        }
        //全都需要，不用转vo
        //序列化转json
        String toJson = JsonUtils.toJson(skuDTOS);
        //写入Redis缓存中
        redisTemplate.opsForValue().set(KEY_PREFIX_SKU + spuId,toJson);
        //返回json，省去controller中序列化
        return toJson;
    }

    @Override
    public String loadCategoriesData(List<Long> ids) {
        //根据id查询
        List<CategoryDTO> categoryDTOS = itemClient.queryCategoryByIds(ids);
        //判断是否为空
        if (categoryDTOS == null || categoryDTOS.size() <= 0) {
            throw new LyException(404, "商品分类不存在！");
        }
        //转换为vo
        List<CategoryVO> categoryVOS = categoryDTOS.stream().map(categoryDTO -> categoryDTO.toEntity(CategoryVO.class)).collect(Collectors.toList());
        //序列化转json
        String toJson = JsonUtils.toJson(categoryVOS);
        //写入Redis缓存中,id,取第三个，三级id
        redisTemplate.opsForValue().set(KEY_PREFIX_CATEGORY + ids.get(2),toJson);
        //返回json，省去controller中序列化
        return toJson;
    }

    @Override
    public String loadBrandData(Long brandId) {
        //根据id查询
        BrandDTO brandDTO = itemClient.queryBrandById(brandId);
        //判断是否为空
        if (brandDTO == null || brandDTO.getId() == null) {
            throw new LyException(404, "品牌不存在！");
        }
        //转换为vo
        BrandVO brandVO = brandDTO.toEntity(BrandVO.class);
        String toJson = saveBrand2Redis(brandVO);
        //返回json，省去controller中序列化
        return toJson;
    }

    public String saveBrand2Redis(BrandVO brandVO) {
        //序列化转json
        String toJson = JsonUtils.toJson(brandVO);
        //写入Redis缓存中
        redisTemplate.opsForValue().set(KEY_PREFIX_BRAND + brandVO.getId(),toJson);
        return toJson;
    }

    @Override
    public String loadSpecData(Long categoryId) {
        //根据id查询
        List<SpecGroupDTO> specGroupDTOS = itemClient.querySpecList(categoryId);
        //判断是否为空
        if (specGroupDTOS == null || specGroupDTOS.size() <= 0) {
            throw new LyException(404, "规格组不存在！");
        }
        //新建一个vo集合
        List<SpecGroupVO> specGroupVOList = new ArrayList<>(specGroupDTOS.size());
        //遍历dto集合
        for (SpecGroupDTO specGroupDTO : specGroupDTOS) {
            //新建一个Vo
            SpecGroupVO specGroupVO = new SpecGroupVO();
            //赋值name
            specGroupVO.setName(specGroupDTO.getName());
            //赋值params
            //获取paramDTO集合
            List<SpecParamDTO> paramDTOS = specGroupDTO.getParams();
            //转换为vo
            List<SpecParamVO> paramVOS = paramDTOS.stream().map(paramDTO -> paramDTO.toEntity(SpecParamVO.class)).collect(Collectors.toList());
            //spceGroupVO赋值params
            specGroupVO.setParams(paramVOS);
            //添加到集合中
            specGroupVOList.add(specGroupVO);
        }
        //序列化转为json字符串
        String toJson = JsonUtils.toJson(specGroupVOList);
        //写入Redis缓存中
        redisTemplate.opsForValue().set(KEY_PREFIX_SPEC + categoryId,toJson);
        //返回json，省去controller中序列化
        return toJson;
    }

    @Override
    public void deleteSkuList(Long spuId) {
        redisTemplate.delete(KEY_PREFIX_SKU + spuId);
    }

    @Override
    public void deleteBrandById(Long brandId) {
        redisTemplate.delete(KEY_PREFIX_BRAND + brandId);
    }
}
